<!-- IconPicker.vue -->
<template>
  <div>
    <el-input
        v-model="selectedIcon"
        placeholder="点击选择图标"
        readonly
        @click="dialogVisible = true"
    >
      <template #suffix>
        <component
            :is="selectedIcon"
            v-if="selectedIcon"
            style="width: 1em; height: 1em; margin-right: 5px"
        />
        <el-icon v-else>
          <arrow-down/>
        </el-icon>
      </template>
    </el-input>

    <el-dialog
        v-model="dialogVisible"
        title="选择图标"
        width="70%"
    >
      <div class="icon-grid">
        <div
            v-for="icon in icons"
            :key="icon.name"
            class="icon-item"
            @click="handleSelect(icon.name)"
        >
          <el-icon :size="26">
            <component :is="icon.component"/>
          </el-icon>
          <span class="icon-name">{{ icon.name }}</span>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const dialogVisible = ref(false)
const selectedIcon = ref('')

// 转换图标数据为数组格式
const icons = ref([])
onMounted(() => {
  icons.value = Object.entries(ElementPlusIconsVue).map(([name, component]) => ({
    name,
    component
  }))
})

const handleSelect = (iconName) => {
  selectedIcon.value = iconName
  dialogVisible.value = false
}
</script>

<style scoped>
.icon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 20px;
}

.icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.3s;
}

.icon-item:hover {
  background-color: #f5f7fa;
  transform: scale(1.1);
}

.icon-name {
  margin-top: 8px;
  font-size: 12px;
  text-align: center;
  word-break: break-all;
}
</style>